package utilities;

import shapes.ShapeSettings;
import nodes.ShapeNode;
import nodes.ShapeNodeList;

public class ShapeNodeListSorter {
	
	ShapeNodeList list;
	
	public ShapeNodeListSorter(ShapeNodeList list) {
		this.list = list;
	}
	
	public ShapeNodeList sortByShapeIDAscending() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().getShapeID() >= second.getData().getShapeID()) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByShapeIDDescending() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().getShapeID() <= second.getData().getShapeID()) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByAreaAscending() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().area() >= second.getData().area()) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByAreaDescending() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().area() <= second.getData().area()) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByFormeType1() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getLineName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getOvalName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getOvalName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getCircleName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getCircleName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getRectangleName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getRectangleName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getSquareName())) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByFormeType2() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getSquareName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getRectangleName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getRectangleName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getCircleName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getCircleName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getOvalName())) {
							list.swap(first, second);
						}
						else if(first.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getOvalName()) && second.getData().getShapeName().equalsIgnoreCase(ShapeSettings.getLineName())) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
	public ShapeNodeList sortByDistance() {
		ShapeNode first = list.getFirstNode();
		ShapeNode second = first;
		
		if(first != null) {
			while(first != null) {
				if(second != null) {
					while(second != null) {
						if(first.getData().getDistance() >= second.getData().getDistance()) {
							list.swap(first, second);
						}
						second = second.getNext();
					}
				}
				first = first.getNext();
				second = first;
			}
		}
		
		return list;
	}
	
}